
public class Grapher{
  private int widgetX1;
  private int widgetY1;
  private int widgetX2;
  private int widgetY2;
  private int h1;
  private int h2;
  private int w1;
  private int[] hourlyData;
  private BufferedReader input;
  private int[][] depthData;
  private int[][] freqData;
  private int depthDataSize = 14;
  private int freqDataSize = 9;
  private int depthMin = 50;
  private int depthMax = 700;
  private int quakeMin = Integer.MAX_VALUE;
  private int quakeMax = Integer.MIN_VALUE;  
  private int freqMin = Integer.MAX_VALUE;
  private int freqMax = Integer.MIN_VALUE; 
  private float fData[]; 
  private int relativePower[];
  private final int axesColor = #00665E;   
  private int textColor = 30; 
  private int pointsColor = 30;   
  private PFont font;
   
  Grapher(){
  }
  
  Grapher(int x1, int y1, int x2, int y2){
    fData = new float[8];
    relativePower = new int[8];
    fData[0] = 22055;
    fData[1] = 155855;
    fData[2] = 70483;
    fData[3] = 225564;
    fData[4] = 54720;
    fData[5] = 4498;
    fData[6] = 483;
    fData[7] = 29;
    relativePower[0] = 1;
    relativePower[1] = 89;
    relativePower[2] = 6;
    relativePower[3] = 32;
    relativePower[4] = 32;
    relativePower[5] = 32;
    relativePower[6] = 32;
    relativePower[7] = 1000;
    this.widgetX1 = x1;
    this.widgetY1 = y1;
    this.widgetX2 = x2;
    this.widgetY2 = y2;
    this.h1 =  int( (float) ( y2 - y1 ) / 3.3);
    this.h2 =  ( (y2 - y1) / 3 ) - h1;
    println(h2);
    this.w1 = ( x2 - x1 ) / 12;
    hourlyData = new int[24];
    depthData = new int[depthDataSize][2];
    freqData = new int[freqDataSize][2];
    font = createFont("Arial", 10);
    textFont(font);
    setup();
    percentagizeList();
  }
  
  private void setup(){
    String inputLine;    
    ArrayList tempList = dc.getData();
    
    for( int i = 0; i < tempList.size(); i ++) {
      DataRow dr = (DataRow) tempList.get(i);
      double temp = dr.getTime();
      float temp1 = (float) temp;
      temp1 /= 10000;
      hourlyData[floor( temp1 ) ]++;
    }
    
    input = createReader("eqdata_depth.csv");
    int i = 0;
    try{
      while( (inputLine = input.readLine()) != null  ) {
        int j = 0;
        String linePieces[] = inputLine.split(",");
        depthData[i][j] = Integer.parseInt( linePieces[0].replaceAll(" ", "") );
        j ++;
        depthData[i][j] = Integer.parseInt( linePieces[1].replaceAll(" ", "") );
        if(depthData[i][j] > quakeMax)
          quakeMax = depthData[i][j];
        else
          if(depthData[i][j] < quakeMin)
            quakeMin = depthData[i][j];
        
        i ++;
      }
    }
    catch(IOException e){
      println(e + " at grapher");
    }
        
    input = createReader("eqdata-freq.csv");
    i = 0;
    try{
      while( (inputLine = input.readLine()) != null  ) {
        int j = 0;
        String linePieces[] = inputLine.split(",");
        freqData[i][j] = Integer.parseInt( linePieces[0].replaceAll(" ", "") );
        j ++;
        freqData[i][j] = Integer.parseInt( linePieces[1].replaceAll(" ", "") );
        if(freqData[i][j] > freqMax)
          freqMax = freqData[i][j];
        else
          if(freqData[i][j] < freqMin)
            freqMin = freqData[i][j];        
        i ++;
      }
    }
    catch(IOException e){
      println(e + " at grapher");
    }
    
  }
       
  
  public void draw(){
    drawGraph(1);
    drawBarGraph(2);
    drawPieChart(3);
  }
  
  private void drawGraph( int pos ){
    if(pos == 0)
      return;
    int plotX1 = widgetX1 + 2 * w1;
    int plotX2 = widgetX2;
    int plotY1 = widgetY1 + (pos-1) * h1 + h2;
    int plotY2 = widgetY1 + pos * h1;
    if( plotY2 > widgetY2 )
      plotY2 = widgetY2;  
      

    
    fill(textColor);
    textSize(10);
    
    textAlign(CENTER, TOP);
    text( "Depth --->", (plotX2 - plotX1)/2 + w1, (plotY2 + h2/4));
    text( "Q\nu\na\nk\ne\ns\n", ( widgetX1 + w1 ), plotY1 + (pos + 1) * h2 );
    
    stroke(axesColor);
    strokeWeight(1);
    noFill();      
    
    line(plotX1, plotY2, plotX2, plotY2);
    line(plotX1, plotY1, plotX1, plotY2);

    int xInterval = (( plotX2 - plotX1 ) / depthDataSize);

    beginShape();

    for( int i = 0 ; i < depthDataSize; i ++ ) {
      float yt = map( depthData[i][1], quakeMin, quakeMax, (float)plotY2, (float) plotY1);
      vertex( plotX1 + i * xInterval, yt );
      if( i == 0 || i == depthDataSize - 1 )
        drawXLabel(depthData[i][0], plotX1 + i * xInterval, plotY2);
      drawDataHighlight(depthData[i][1], plotX1 + i * xInterval, (int)yt);
    }    

    endShape();
  }
  
  void drawXLabel(int valuex, int x, int plotY1) {
    stroke(axesColor);
    strokeWeight(1);
    line(x, plotY1, x, plotY1+5);  
    fill(textColor);
    textSize(7);
    textAlign(CENTER, CENTER);
    text(ceil(valuex), x, plotY1 + 10);
    noFill();
  }
  
  private void drawDataHighlight(int val, int x, int y){
    if( dist(mouseX, mouseY, x, y) < 5 ) {
      stroke(pointsColor);
      strokeWeight(3);
      point(x,y);
      textSize(11);
      fill(textColor);
      text( val, x, y - 8 );
      noFill();
    }
  }
  
  
  private void drawBarGraph( int pos ){
    if(pos == 0)
      return;
    int plotX1 = widgetX1 + 2 * w1;
    int plotX2 = widgetX2;
    int plotY1 = widgetY1 + (pos-1) * h1 + h2 + 50;
    int plotY2 = widgetY1 + pos * h1;
    if( plotY2 > widgetY2 )
      plotY2 = widgetY2;  
      
    fill(textColor);
    textSize(10);
    
    textAlign(LEFT, CENTER );
    text( "Magnitude --->", (plotX2 - plotX1)/2, (plotY2 + h2/4));
    
    textAlign(CENTER, TOP);
    text( "R\ne\nl\n\np\no\nw\ne\nr\n", ( widgetX1 +  w1 ), plotY1 + (pos-2) * h2 );
    
    stroke(axesColor);
    strokeWeight(1);
    noFill();      
    
    line(plotX1, plotY2, plotX2, plotY2);
    line(plotX1, plotY1, plotX1, plotY2);

    int w = (( plotX2 - plotX1 ) / relativePower.length);
    
    for(int i = 0; i < relativePower.length; i ++ ){
      float y = map(relativePower[i], 1, 1000, plotY2, plotY1);
      rectMode(CORNERS);
      fill(mLegend.getColor(i));
      rect( plotX1 + i * w + 2, plotY2, plotX1 + (i+1) * w, y);
      rectMode(CORNER);
    }
  }
  
    void percentagizeList(){
      float sum = 0 ;
      for (int i= 0; i < fData.length; i++){
        sum += fData[i];
//        println(sum);
      } 
      for(int i = 0; i < fData.length; i++ ) {
        fData[i] /= sum;
      }
    }

  private void drawPieChart( int pos ){
    if(pos == 0)
      return;
    int plotX1 = widgetX1 + 3 * w1;
    int plotX2 = widgetX2;
    int plotY1 = widgetY1 + (pos-1) * h1 + h2;
    int plotY2 = widgetY1 + pos * h1;
    if( plotY2 > widgetY2 )
      plotY2 = widgetY2;  
      
    int widgetWidth = widgetX2 - widgetX1 + w1;
    
    int w = widgetWidth/2;
    int h = plotY2 - (plotY2 - plotY1)/2;
    
    fill(textColor);
    textSize(10);
    
    textAlign(CENTER, TOP);
    text( "F\nr\ne\nq\n\nD\ni\ns\nt\n", ( widgetX1 + w1 ), plotY1 + (pos-1) * h2 );
  
    stroke(axesColor);
    strokeWeight(1);
    noFill();      
    
    float lastAng = 0;    
    
    for(int i = 0; i < fData.length; i++) {
      float value = fData[i] * 360;
      float diameter = widgetWidth - 5 * w1;
      fill(mLegend.getColor(i));
      arc(w,h, diameter, diameter, lastAng, lastAng + radians(value));
      lastAng += radians(value);
    }  
  }
}

